package cn.javaxc.mvcwork.modules.file;
import cn.javaxc.mvcwork.framework.actions.TActionContext;
import cn.javaxc.mvcwork.framework.beans.TFormBean;
import cn.javaxc.model.base.*;

import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import java.sql.*;
import java.text.DateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import cn.javaxc.mvcwork.util.common.*;
import cn.javaxc.mvcwork.util.helper.*;


public class FileItemVersion extends TFormBean {

  public final static String fs = System.getProperty("file.separator");

  private Object id = -1;
  private Object itemId = -1;
  private String subject = "";
  private String clientFilename = "";
  private String filename = "";
  private String directory = "";
  private int size = 0;
  private double version = 0;
  private String image = null;
  private boolean enabled = false;
  private int downloads = 0;
  private java.sql.Timestamp entered = null;
  private int enteredBy = -1;
  private String enteredByString = "";
  private java.sql.Timestamp modified = null;
  private int modifiedBy = -1;
  private String modifiedByString = "";
  private boolean allowPortalAccess = false;


  
  public FileItemVersion() {
  }


  
  public void queryRecord( int itemId,TActionContext context)  {
    String sql =
        "SELECT v.* " +
        "FROM ts_files_version v " +
        "WHERE v.version_id = ? ";
    SQLMap pst = FrameHelper.getBaseSQL().createStatement();
    pst.setInt(1, itemId);
    SQLList<SQLMap> rs = pst.executeQuery(sql,"");
    if (rs.size()>0) {
      buildRecord(rs.get(0));
    } else {
      throw new RuntimeException("File version record not found.");
    }
  }


  
  public FileItemVersion(SQLMap rs)  {
    buildRecord(rs);
  }


  
  public void setId(int tmp) {
    this.id = tmp;
  }


  
  public void setId(String tmp) {
    this.id = Integer.parseInt(tmp);
  }


  
  public void setItemId(Object tmp) {
    this.itemId = tmp;
  }



  public Object getItemId() {
    return itemId;
  }
  
  
  public void setSubject(String tmp) {
    this.subject = tmp;
  }


  
  public void setClientFilename(String tmp) {
    this.clientFilename = tmp;
  }


  
  public void setFilename(String tmp) {
    this.filename = tmp;
  }


  
  public void setDirectory(String tmp) {
    this.directory = tmp;
  }


  
  public void setSize(int tmp) {
    this.size = tmp;
  }


  
  public void setSize(String tmp) {
    this.size = Integer.parseInt(tmp);
  }


  
  public void setVersion(double tmp) {
    this.version = tmp;
  }


  
  public void setVersion(String tmp) {
    this.version = Double.parseDouble(tmp);
  }


  
  public void setImage(String tmp) {
    this.image = tmp;
  }


  
  public void setEntered(java.sql.Timestamp tmp) {
    this.entered = tmp;
  }


  
  public void setEntered(String tmp) {
    this.entered = DatabaseUtils.parseTimestamp(tmp);
  }


  
  public void setEnteredBy(int tmp) {
    this.enteredBy = tmp;
  }


  
  public void setEnteredBy(String tmp) {
    this.enteredBy = Integer.parseInt(tmp);
  }


  
  public void setEnteredByString(String tmp) {
    this.enteredByString = tmp;
  }


  
  public void setModified(java.sql.Timestamp tmp) {
    this.modified = tmp;
  }


  
  public void setModified(String tmp) {
    this.modified = DatabaseUtils.parseTimestamp(tmp);
  }


  
  public void setModifiedBy(int tmp) {
    this.modifiedBy = tmp;
  }


  
  public void setModifiedBy(String tmp) {
    this.modifiedBy = Integer.parseInt(tmp);
  }


  
  public void setModifiedByString(String tmp) {
    this.modifiedByString = tmp;
  }


  
  public void setDownloads(int tmp) {
    downloads = tmp;
  }


  
  public void setDownloads(String tmp) {
    downloads = Integer.parseInt(tmp);
  }


  
  public void setEnabled(boolean tmp) {
    this.enabled = tmp;
  }


  
  public void setEnabled(String tmp) {
    this.enabled = ("on".equalsIgnoreCase(tmp) || "true".equalsIgnoreCase(tmp));
  }


  
  public Object getId() {
    return id;
  }


  
  public String getSubject() {
    return subject;
  }


  
  public String getClientFilename() {
    return clientFilename;
  }


  
  public String getExtension() {
    if (clientFilename.indexOf(".") > 0) {
      return clientFilename.substring(clientFilename.lastIndexOf(".")).toLowerCase();
    } else {
      return "";
    }
  }


  
  public String getFilename() {
    return filename;
  }


  
  public String getDirectory() {
    return directory;
  }


  
  public int getSize() {
    return size;
  }


  
  public int getRelativeSize() {
    int newSize = (size / 1000);
    if (newSize == 0) {
      return 1;
    } else {
      return newSize;
    }
  }


  
  public double getVersion() {
    return version;
  }


  
  public String getVersionNextMajor() {
    return (((int) version + 1) + ".0");
  }


  
  public String getVersionNextMinor() {
    String value = String.valueOf(version);
    if (value.indexOf(".") > -1) {
      value = value.substring(0, value.indexOf(".") + 2);
    }

    String newVersion = (new java.math.BigDecimal(value).add(
        new java.math.BigDecimal(".1"))).toString();
    if (Double.parseDouble(newVersion) > (Double.parseDouble(
        getVersionNextMajor()))) {
      return getVersionNextMajor();
    } else {
      return newVersion;
    }
  }


  
  public String getVersionNextChanges() {
    String newVersion = (new java.math.BigDecimal("" + version).add(
        new java.math.BigDecimal(".01"))).toString();
    return newVersion;
  }


  
  public String getImage() {
    return image;
  }


  
  public String getImageTag() {
    return FileItem.getImageTag(
        image, "", FileItem.getExtension(clientFilename));
  }


  
  public java.sql.Timestamp getEntered() {
    return entered;
  }


  
  public String getEnteredString() {
    try {
      return DateFormat.getDateInstance(3).format(entered);
    } catch (NullPointerException e) {
    }
    return "";
  }


  
  public String getEnteredDateTimeString() {
    try {
      return DateFormat.getDateTimeInstance(3, 3).format(entered);
    } catch (NullPointerException e) {
    }
    return "";
  }


  
  public int getEnteredBy() {
    return enteredBy;
  }


  
  public String getEnteredByString() {
    return enteredByString;
  }


  
  public java.sql.Timestamp getModified() {
    return getEntered();
  }


  
  public String getModifiedString() {
    try {
      return DateFormat.getDateInstance(3).format(modified);
    } catch (NullPointerException e) {
    }
    return "";
  }


  
  public String getModifiedDateTimeString() {
    try {
      return DateFormat.getDateTimeInstance(3, 3).format(modified);
    } catch (NullPointerException e) {
    }
    return "";
  }

  public Timestamp getModificationDate() {
    if (modified != null) {
      return modified;
    }
    if (entered != null) {
      return entered;
    }
    return new Timestamp((new java.util.Date()).getTime());
  }

  
  public int getModifiedBy() {
    return modifiedBy;
  }


  
  public String getModifiedByString() {
    return modifiedByString;
  }


  
  public int getDownloads() {
    return downloads;
  }


  
  public boolean getEnabled() {
    return enabled;
  }


  
	public boolean getAllowPortalAccess() {
		return allowPortalAccess;
	}


	
	public void setAllowPortalAccess(boolean allowPortalAccess) {
		this.allowPortalAccess = allowPortalAccess;
	}

  
  public void setAllowPortalAccess(String allowPortalAccess) {
    this.allowPortalAccess = DatabaseUtils.parseBoolean(allowPortalAccess);
  }

	
  public boolean insert() throws Exception {
//    StringBuffer sql = new StringBuffer();
//    sql.append("INSERT INTO\n ts_files_version ");
//    sql.append(
//        "(item_id, subject, client_filename, filename, version, size, ");
//    sql.append("enabled, downloads, ");
//    if (!StringUtils.isNullData(id)) {
//      sql.append("version_id, ");
//    }
//    sql.append("entered, modified, enteredBy, modifiedBy, allow_portal_access )\n ");
//    sql.append("VALUES (:item_id, :subject, :client_filename, :filename, :version, :size,:enabled, :downloads, ");
//    if (!StringUtils.isNullData(id)) {
//      sql.append(":version_id, ");
//    }
//    if (entered != null) {
//      sql.append(":entered, ");
//    } else {
//      sql.append("CURRENT_TIMESTAMP,");
//    }
//    if (modified != null) {
//      sql.append(":modified, ");
//    } else {
//      sql.append("CURRENT_TIMESTAMP,");
//    }
//    sql.append(":enteredBy, :modifiedBy, :allow_portal_access) ");
	SQLMap emt=new SQLMap();
	
    emt.setObject("item_id", itemId);
    emt.setString("subject", subject);
    emt.setString("client_filename", clientFilename);
    emt.setString("filename", filename);
    emt.setDouble("version", version);
    emt.setInt("size", size);
    emt.setBoolean("enabled", enabled);
    emt.setInt("downloads", downloads);
    if (!StringUtils.isNullData(id)) {
      emt.setObject("version_id", id);
    }
    if (entered != null) {
      emt.setTimestamp("entered", entered);
    }
    if (modified != null) {
      emt.setTimestamp("modified", modified);
    }
    emt.setInt("enteredBy", enteredBy);
    emt.setInt("modifiedBy", modifiedBy);
    emt.setBoolean("allow_portal_access", allowPortalAccess);
//	emt.execute(sql.toString());
	IBaseDAO dao=FrameHelper.getBaseDAO();
	dao.insertFileVersion(emt);
	////////////////////////////
//    id = DatabaseUtils.getCurrVal(null,"project_files_version_version_id_seq", -1);
    return true;
  }


  


  
  public synchronized boolean updateCounter()  {
    FileDownloadLog thisLog = new FileDownloadLog();
    thisLog.setItemId(itemId);
    thisLog.setVersion(version);
    thisLog.setUserId(enteredBy);
    thisLog.setFileSize(size);
    return thisLog.updateCounter();
  }

  
  public boolean delete( String baseFilePath)  {


    
    return true;
  }

 

  
  private void buildRecord(SQLMap rs)  {
    id = rs.getInt("version_id");
    itemId = rs.getInt("item_id");
    clientFilename = rs.getString("client_filename");
    filename = rs.getString("filename");
    subject = rs.getString("subject");
    size = rs.getInt("size");
    version = rs.getDouble("version");
    enabled = rs.getBoolean("enabled");
    downloads = rs.getInt("downloads");
    entered = rs.getTimestamp("entered");
    enteredBy = rs.getInt("enteredBy");
    modified = rs.getTimestamp("modified");
    modifiedBy = rs.getInt("modifiedBy");
    allowPortalAccess = rs.getBoolean("allow_portal_access");
  }
}
